package com.bawei.module_visitor

import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.base.utils.ToastUtils
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.module_visitor.databinding.ActivityVisitorAddBinding
import com.bawei.module_visitor.model.VisitorAddBody
import com.bawei.module_visitor.viewmodel.VisitorViewModel
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.TouchUtils
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = Router.Ui.VisitorAddActivity)
class VisitorAddActivity : BaseActivity<VisitorViewModel,ActivityVisitorAddBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_visitor_add
    override val mViewModel: VisitorViewModel by viewModels()
    val listOf = mutableListOf<String>()

    override fun initView() {
        mBinding.addVisitCarNum.isEnabled = false
        mBinding.addVisitDataView.setOnClickListener {
            DatePickerDialog(this,
                { _, year, month, dayOfMonth ->
                    mBinding.addVisitData.text = "$year-${month + 1}-$dayOfMonth"
                }, 2021, 1, 1).show()
        }
        mBinding.addVisitStartTime.setOnClickListener {
            TimePickerDialog(this,
                { _, hourOfDay, minute ->
                    mBinding.addVisitStartTime.text = "$hourOfDay:$minute"
                }, 10, 10, true).show()
        }
        mBinding.addVisitEndTime.setOnClickListener {
            TimePickerDialog(this,
                { _, hourOfDay, minute ->
                    mBinding.addVisitEndTime.text = "$hourOfDay:$minute"
                }, 10, 10, true).show()
        }
        mBinding.visitBack.setOnClickListener {
            finish()
        }
        mBinding.addVisitParkingSwitch.setOnCheckedChangeListener { _, isChecked ->
            mBinding.addVisitCarNum.isEnabled = isChecked
        }
        // 定义车牌号正则表达式，支持普通民用车辆、新能源车、以及挂学警港澳等特殊车牌格式
        val regex = "^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][·]?[A-HJ-NP-Z0-9]{4,5}(?:[A-HJ-NP-Z0-9]|[挂学警港澳])?\$"
        mBinding.addVisitSave.setOnClickListener {

            if (mBinding.addVisitParkingSwitch.isChecked) {
                if (mBinding.addVisitCarNum.text.toString().isEmpty()) {
                    mBinding.addVisitCarNum.error = "请填写车牌号"
                    showToast("请输入车牌号")
                    return@setOnClickListener
                }
                if (!mBinding.addVisitCarNum.text.toString().matches(regex.toRegex())) {
                    if (mBinding.addVisitCarNum.text.toString().length < 7) {
                        mBinding.addVisitCarNum.error = "车牌号格式不正确"
                        showToast("车牌号格式不正确")
                        return@setOnClickListener
                    }
                }
            }
            if (mBinding.addVisitPurpose.text.toString().isEmpty()) {
                mBinding.addVisitPurpose.error = "请填写访问事由"
                showToast("请输入访问事由")
                return@setOnClickListener
            }
            if (mBinding.addVisitName.text.toString().isEmpty()) {
                mBinding.addVisitName.error = "请填写访客姓名"
                showToast("请输入访客姓名")
                return@setOnClickListener
            }
            if (mBinding.addVisitPhoneNum.text.toString().isEmpty()) {
                mBinding.addVisitPhoneNum.error = "请填写访客手机号"
                showToast("请输入访客手机号")
                return@setOnClickListener
            }
            if (!RegexUtils.isMobileExact(mBinding.addVisitPhoneNum.text.toString())) {
                mBinding.addVisitPhoneNum.error = "请填写正确的手机号"
                showToast("请输入正确的手机号")
                return@setOnClickListener
            }
            if (mBinding.addVisitCardNum.text.toString().isEmpty()) {
                mBinding.addVisitCardNum.error = "请填写访客身份证号"
                showToast("请输入访客身份证号")
                return@setOnClickListener
            }
            if (!RegexUtils.isIDCard18(mBinding.addVisitCardNum.text.toString())) {
                mBinding.addVisitCardNum.error = "请填写正确的身份证号"
                showToast("请输入正确的身份证号")
                return@setOnClickListener
            }
            if (mBinding.addVisitEmployee.text.toString().isEmpty()) {
                mBinding.addVisitEmployee.error = "请填写访问对象"
                showToast("请输入访问对象")
                return@setOnClickListener
            }

            val param = VisitorAddBody(
                appointmentTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()),
                requestParking = mBinding.addVisitParkingSwitch.isChecked,
                employeeId = 1,
                idNumber = mBinding.addVisitCardNum.text.toString().toLong(),
                plateNumber = mBinding.addVisitCarNum.text.toString(),
                visitStatus = "待审核",
                visitorName = mBinding.addVisitName.text.toString(),
                id = 0,
                purpose = mBinding.addVisitPurpose.text.toString(),
                attachments = listOf,
                approved = false,
                phoneNumber = mBinding.addVisitPhoneNum.text.toString().toLong(),
            )
           mViewModel.getVisitorApplyAdd(param)
        }
    }

    override fun initListener() {
        mViewModel.visitorAddEvent.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {}
                Loading -> {}
                Success -> {
                    ToastUtils.showLong(this,"保存预约来访成功")
                    finish()
                }
            }
        }
    }

    override fun initData() {

    }

}